<template>
  <div class="login-wrap">
    <div class="ms-title">点餐系统</div>
    <div>
      <el-form class="ms-login" :model="loginForm" ref="loginForm" :rules="rules" label-width="50px" label-position="right">
        <el-form-item label="手机" prop="phone" style="font-weight: bold">
          <el-input v-model="loginForm.phone" clearable placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" style="font-weight: bold">
          <el-input v-model="loginForm.password" show-password clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="login-btn" @click="login('loginForm')">登录</el-button>
        </el-form-item>
        <el-form-item>
          <p class="register">
            <router-link to="/Register">注册新用户</router-link>
          </p>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import request from "../../request";

export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        phone: '',
        password: ''
      },
      rules: {
        phone: [
          {required: true, message: '手机号不能为空', trigger: 'blur'},
          {max: 11, min: 11, message: '手机号为11位', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '密码不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    login (loginForm) {
      this.$refs[loginForm].validate((valid) => {
        if (valid) {
          if (isNaN(this.loginForm.phone)) {
            this.$message.error("手机号只能为数字！")
            return
          }
          for (let i of this.loginForm.password) {
            if (i === ' ') {
              this.$message.error("密码不能包含空格！")
              return
            }
          }
          this.$request.post('/user/login', {
              "phone": this.loginForm.phone,
              "password": this.loginForm.password
            }).then((result) => {
            if (result.code === 200) {
              // 登录成功
              sessionStorage.setItem('phone', result.data.phone)
              sessionStorage.setItem('user_name', result.data.username)
              sessionStorage.setItem('is_admin', result.data.isAdmin)
              sessionStorage.setItem('avatar', result.data.photo)
              if (sessionStorage.getItem('is_admin') === '0') {
                this.$router.push('/customer/customerDish')
              } else {
                this.$router.push('/admin/adminDish')
              }
            } else {
              // 登录失败
              this.$message.error(result.msg)
            }
          })
        } else {
          this.$message.error("数据不合法！")
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.login-wrap {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(-225deg, #B7F8DB 0%, #50A7C2 100%);
}

.ms-title {
  width: 100%;
  padding-top: 30px;
  font-size: 30px;
}

.ms-login {
  margin-top: 30px;
  padding: 50px;
  width: 300px;
  height: 240px;
  border: 1px solid;
  border-radius: 5px;
  margin-left: auto;
  margin-right: auto;
}

.login-btn {
  width: 100%;
  height: 36px;
  text-align: center;
}

.register {
  font-size: 14px;
  line-height: 30px;
  color: #999;
  cursor: pointer;
  float: right;
}
</style>
